package com.study.dataStructure.util;

import java.io.File;

/**
 * 清理maven仓库
 * @author xuzhao
 *
 */
public class MavenClean {

//    private static int emptyDirectoryNum = 0;
//    private static int lastUpdateNum = 0;
//
//    private int emptyNum = 0;
    /**
     * 要清理的内容包括
     * 1. 所有的.lastUpdated 结尾的文件
     * 2. 清理之后的所有空文件夹
     */

    public static void main(String[] args) {
       /* final String mavenRepositoryPath = "D:\\maven_repository";
        File f = new File(mavenRepositoryPath);
        MavenClean clean = new MavenClean();
        //while(clean.emptyNum!=0&&clean.emptyNum!=-1) {
        //这里一次执行清理不完 多次执行直到打印出0
        clean.cleanLastUpdate(f);
        clean.cleanEmptyDirectory(f);
        System.out.println("删除文件 "+ lastUpdateNum+"个, 删除空文件夹 "+emptyDirectoryNum+"个");
        System.out.println(clean.emptyNum);
        //}*/
        clean();
    }

    public static void clean(){
        final String mavenRepositoryPath = "D:\\maven_repository";

        int emptyDirectoryNum = 0;
        int lastUpdateNum = 0;
        int emptyNum = 0;

        File f = new File(mavenRepositoryPath);
        MavenClean clean = new MavenClean();
        //while(clean.emptyNum!=0&&clean.emptyNum!=-1) {
        //这里一次执行清理不完 多次执行直到打印出0
        clean.cleanLastUpdate(f,lastUpdateNum);
        clean.cleanEmptyDirectory(f,emptyNum,emptyDirectoryNum);
        if(lastUpdateNum==0 && emptyDirectoryNum==0 && emptyNum==0){
            System.out.println( "本地仓库: "+mavenRepositoryPath+" 已清理完毕");
            return;
        }else{
            System.out.println("删除文件 "+ lastUpdateNum+"个, 删除空文件夹 "+emptyDirectoryNum+"个");
            System.out.println(emptyNum);
            clean();
        }

    }

    /**
     * 删除所有.lastUpdated结尾的文件
     * @param f
     */
    public  void cleanLastUpdate(File f,int lastUpdateNum) {
        if(f!=null) {
            if(f.getName().endsWith("lastUpdated")) {
                lastUpdateNum++;
                System.out.println("要删除的文件是："+f.getAbsolutePath());
                f.delete();
            }
            if(f.isDirectory()) {
                File[] fs = f.listFiles();
                for(File fItem:fs) {
                    cleanLastUpdate(fItem,lastUpdateNum);
                }
            }
        }

    }
    /**
     * 删除所有的空文件夹
     * @param f
     */
    public  int cleanEmptyDirectory(File f,int emptyNum,int emptyDirectoryNum) {
        if(f!=null&&f.isDirectory()) {
            File[] fs = f.listFiles();
            if(fs.length==0) {
                emptyDirectoryNum++;
                emptyNum++;
                System.out.println(f.getAbsolutePath() +" 是空文件夹被删除");
                f.delete();
            }else {
                for(File fc:fs) {
                    cleanEmptyDirectory(fc,emptyNum,emptyDirectoryNum);
                }
            }
        }
        return emptyNum;

    }
}